package Homework4;

class NotTriangle extends Exception {
    public NotTriangle(String message) {
        super(message);
    }
}

public class Triangle {
    private double _a;
    private double _b;
    private double _c;

    Triangle(double a, double b, double c) {
        _a = a;
        _b = b;
        _c = c;
    }

    public double area() throws NotTriangle {
        if ((_a + _b <= _c) || (_a + _c <= _b) || (_b + _c <= _a)) {
            throw new NotTriangle("非三角形");
        }
        double s = (_a + _b + _c) / 2;
        return Math.sqrt(s * (s - _a) * (s - _b) * (s - _c));
    }

    public static void main(String[] args) {
        try {
            Triangle triangle1 = new Triangle(3, 4, 5);
            double area1 = triangle1.area();
            System.out.println("三角形的面积是: " + area1);
        } catch (NotTriangle e) {
            System.out.println(e.getMessage());
        }
        try {
            Triangle triangle2 = new Triangle(1.0, 1.0, 2.0);
            double area2 = triangle2.area();
            System.out.println("三角形的面积是: " + area2);
        } catch (NotTriangle e) {
            System.out.println(e.getMessage());
        }
    }
}
